package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.CourceSessionUser;
import com.atguigu.gmall.publisher.service.CourceSessionUserService;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CourceSessionUserController {
    @Autowired
    private CourceSessionUserService courceSessionUserService;
    @RequestMapping("/CourceSessionUser")
    public String getCourceSessionUser(
    @RequestParam(value = "date",defaultValue = "1")  Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<CourceSessionUser> visitorPerHrStats = courceSessionUserService.getVisitorPerHrStats(date);
        if (visitorPerHrStats == null || visitorPerHrStats.size() == 0){
            return "";
        }
        CourceSessionUser oneVisitorStats = null;
        CourceSessionUser twoVisitorStats = null;
        CourceSessionUser threeVisitorStats = null;
        CourceSessionUser fourVisitorStats = null;
        for (CourceSessionUser visitorPerHrStat : visitorPerHrStats) {
            String sc = visitorPerHrStat.getSc();
            if ("1".equals(sc)) {
                oneVisitorStats = visitorPerHrStat;
            } else if ("2".equals(sc)) {
                twoVisitorStats = visitorPerHrStat;
            } else if ("3".equals(sc)) {
                threeVisitorStats = visitorPerHrStat;
            } else if ("4".equals(sc)) {
                fourVisitorStats = visitorPerHrStat;
            }
        }
            //拼接json字符串
            String json = "{\"status\":0,\"data\":{\"total\":5," +
                    "\"columns\":[" +
                    "{\"name\":\"类别\",\"id\":\"type\"}," +
                    "{\"name\":\"来源1\",\"id\":\"1\"}," +
                    "{\"name\":\"来源2\",\"id\":\"2\"}," +
                    "{\"name\":\"来源3\",\"id\":\"3\"}," +
                    "{\"name\":\"来源4\",\"id\":\"4\"}]," +
                    "\"rows\":[" +
                    "{\"type\":\"独立访客数(人)\",\"1\":" + oneVisitorStats.getUvCt() + ",\"2\":" + twoVisitorStats.getUvCt() + ",\"3\":" + threeVisitorStats.getUvCt() + ",\"4\":" + fourVisitorStats.getUvCt() + "}," +
                    "{\"type\":\"总访问页面数(次)\",\"1\":" + oneVisitorStats.getPvCt() + ",\"2\":" + twoVisitorStats.getPvCt() + ",\"3\":" + threeVisitorStats.getPvCt() + ",\"4\":" + fourVisitorStats.getPvCt() + "}," +
                    "{\"type\":\"跳出率(%)\",\"1\":" + oneVisitorStats.getUjRate() + ",\"2\":" + twoVisitorStats.getUjRate() + ",\"3\":" + threeVisitorStats.getUjRate() + ",\"4\":" + fourVisitorStats.getUjRate() + "}," +
                    "{\"type\":\"平均在线时长(秒)\",\"1\":" + oneVisitorStats.getAvgDurSum() + ",\"2\":" + twoVisitorStats.getAvgDurSum() + ",\"3\":" + threeVisitorStats.getAvgDurSum() + ",\"4\":" + fourVisitorStats.getAvgDurSum() + "}," +
                    "{\"type\":\"平均访问页面数(人次)\",\"1\":" + oneVisitorStats.getAvgPvCt() + ",\"2\":" + twoVisitorStats.getAvgPvCt() + ",\"3\":" + threeVisitorStats.getAvgPvCt() + ",\"4\":" + fourVisitorStats.getAvgPvCt() + "}]}}";

            return json;
    }
}
